//
//  LKBankListViewController.m
//  Mideleague_Agent
//
//  Created by Zhiwang on 2020/6/8.
//  Copyright © 2020 Zhiwang. All rights reserved.
//

#import "LKBankListViewController.h"
#import "LKSearchBar.h"



@implementation  LKBankListCell


-(void)setHeaderTitle:(NSString *)headerTitle{
    _headerTitle = headerTitle;
    self.textLabel.text = _headerTitle;
}

@end



@interface LKBankListViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>

@property(nonatomic,strong)LKBaseTableview *tableView;
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,strong)LKSearchBar *mySearchBar;
@property(nonatomic,strong)NSMutableArray *bankNumberArray;
@end

@implementation LKBankListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"银行列表";

    if (self.isBankAccount) {
        [self setUpSearchBar];
        [self createTableview];
        [self getListDataWithStatusWithKeyWords:@""];
    }else{
        [self setUpSearchBar];
        [self createTableview];
        [self getBankBranchListDataWithStatusWithKeyWords:@""];
    }

    // Do any additional setup after loading the view.
}
//设置搜索框
-(void)setUpSearchBar{
        LKSearchBar *searchBar = [[LKSearchBar alloc] init];
        [self.view addSubview:searchBar];
        [searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view);
            make.height.mas_equalTo(30);
            make.top.equalTo(self.view.mas_top).offset(NAVIGATION_BAR_HEIGHT + 5);
            make.width.mas_equalTo(WIDTH);
        }];
        UIButton *cancel = (UIButton *)[self findViewWithClassName:NSStringFromClass([UIButton class]) inView:searchBar];
        [cancel setTitle:@"取消" forState:UIControlStateNormal];
        [cancel setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        //默认提示文字
        searchBar.placeholder = @"输入银行名称";
        searchBar.delegate = self;
        self.mySearchBar = searchBar;
}
-(void)createTableview{
    self.tableView = [LKBaseTableview setTableviewWithFrame:CGRectZero style:UITableViewStyleGrouped registercellClasss:@[@"LKBankListCell"]];
    [self.tableView registerClass:[LKBankListCell class] forCellReuseIdentifier:@"LKBankListCell"];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    _tableView.backgroundColor = viewDefualtColor;
    [self.view addSubview:_tableView];
    if (self.isBankAccount) {
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
            make.top.equalTo(self.view).offset(NAVIGATION_BAR_HEIGHT + 40);
            make.bottom.equalTo(self.view).offset(0);
        }];
    }else{
           [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
            make.top.equalTo(self.view).offset(NAVIGATION_BAR_HEIGHT + 40);
            make.bottom.equalTo(self.view).offset(0);
        }];
    }

    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        // Fallback on earlier versions
    }
    [self.tableView reloadData];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LKBankListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKBankListCell"];
    cell.headerTitle = self.dataArray[indexPath.section];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40.0f;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *header = [[UIView alloc] init];
    return header;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 1.0f;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footerView = [[UIView alloc] init];
    return footerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.0000000001f;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.bankBlock) {
    self.bankBlock(self.dataArray[indexPath.section],self.bankNumberArray[indexPath.section]);
    }
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - UISearchBarDelegate

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    return YES;
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    searchBar.showsCancelButton = YES;
    
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [self.dataArray removeAllObjects];
    if (self.isBankAccount) {
        [self getListDataWithStatusWithKeyWords:searchBar.text];

    }else{
        [self getBankBranchListDataWithStatusWithKeyWords:searchBar.text];
    }
    [searchBar resignFirstResponder];
    NSLog(@"SearchButton");
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    searchBar.showsCancelButton = NO;
    searchBar.text = @"";
    if (self.isBankAccount) {
        [self getListDataWithStatusWithKeyWords:searchBar.text];

    }else{
        [self getBankBranchListDataWithStatusWithKeyWords:searchBar.text];
    }
    UIButton *cancelBtn = [searchBar valueForKey:@"cancelButton"]; //首先取出cancelBtn
    cancelBtn.enabled = YES; //把enabled设置为yes
    [searchBar resignFirstResponder];
}
-(NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

-(NSMutableArray *)bankNumberArray{
    if (_bankNumberArray == nil) {
        _bankNumberArray = [[NSMutableArray alloc] init];
    }
    return _bankNumberArray;
}
/// 获取银行的数据列表
/// @param keyWords 搜索字
-(void)getListDataWithStatusWithKeyWords:(NSString *)keyWords{

    NSDictionary *param = @{@"token":[LKUserInforTool account].token,@"keywords":keyWords};
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:MyShopOpenBankListUrl header:[LKUserInforTool account].token parameters:param progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        NSArray *dataAray = responseObject[@"data"];
        for (NSDictionary *dic in dataAray) {
            [self.dataArray addObject:dic[@"bank_account"]];
            [self.bankNumberArray addObject:@""];
        }
        [self.tableView reloadData];
    } failure:^(NSString * _Nullable errorMessage) {
        
    }];
}

/// 获取支行
/// @param keyWords 关键字
-(void)getBankBranchListDataWithStatusWithKeyWords:(NSString *)keyWords{

    if ([self.bankProvince containsString:@"市"]) {
        self.banCity = [self.bankProvince mutableCopy];
        self.bankProvince = [[self.bankProvince componentsSeparatedByString:@"市"] firstObject];
    }
    NSDictionary *param = @{@"province_area":self.bankProvince,@"city_area":self.banCity,@"keywords":keyWords,@"bank_account":self.bankAccount};
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:MyShopOpenBankBranchListUrl header:[LKUserInforTool account].token parameters:param progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        NSArray *dataAray = responseObject[@"data"];
        for (NSDictionary *dic in dataAray) {
            [self.dataArray addObject:dic[@"branch_account"]];
            [self.bankNumberArray addObject:dic[@"bank_num"]];
        }
        [self.tableView reloadData];
    } failure:^(NSString * _Nullable errorMessage) {
        
    }];
}
- (UIView *)findViewWithClassName:(NSString *)className inView:(UIView *)view{
    Class specificView = NSClassFromString(className);
    if ([view isKindOfClass:specificView]) {
        return view;
    }
    
    if (view.subviews.count > 0) {
        for (UIView *subView in view.subviews) {
            UIView *targetView = [self findViewWithClassName:className inView:subView];
            if (targetView != nil) {
                return targetView;
            }
        }
    }
    
    return nil;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
